メッセージメニューを用いてメッセージのcontentをUnicode Codeunitsで表現して送信するサンプル
https://gyazo.com/ec7b1836e7b4e4e57e7e2bbf51cd2fa0
code:js
const Discord = require("discord.js");
const GUILD_ID = "494780225280802817"; // command deployed to global if undefined
function string_to_codeunits(str) {
return str.split("").map(e => \\u${e.charCodeAt(0).toString(16).padStart(4, "0")});
}
const client = new Discord.Client({
intents: 0
});
async function on_ready() {
await client.application.commands.set([{
name: "unicode codeunits",
type: "MESSAGE"
}], GUILD_ID);
}
client.on("ready", () => {
on_ready().catch(err => console.error(err));
});
/**
*
* @param {Discord.ContextMenuInteraction} interaction
*/
async function on_context_menu_interaction(interaction) {
const message = interaction.options.getMessage("message");
Discord.Formatters.codeBlock(string_to_codeunits(message.content ?? "").join("")),
{
char: "",
prepend: "`\n",
append: "\n`"
}
);
await interaction.reply(head);
for (const m of tail) {
await interaction.followUp(m);
}
}
/**
*
* @param {Discord.Interaction} interaction
*/
async function on_interaction(interaction) {
if (!interaction.isContextMenu()) {
return;
}
await on_context_menu_interaction(interaction);
}
client.on("interactionCreate", (interaction) => {
on_interaction(interaction).catch(err => console.error(err));
});
client.login();
解説
interaction.options.getMessage("message")
ContextMenuで選択されたメッセージはこのように取得する